
/**
 * 生成日历数据
 * @param { Date } date 
 */
export default function CalendarData(date = new Date()) {
    this.date = date
}

CalendarData.prototype.getDayCount = function () {
    let year = this.date.getFullYear()
    let month = this.date.getMonth()
    let day = this.date.getDate()
    let week = this.date.getDay()
    let newDate = new Date(year, month + 1, 0)
    return {
        year,
        month,
        day,
        week: this.weekContrast(week),
        dayCount: newDate.getDate(),
    }
}

/**
 * 解析年月日
 * @param { Date } date 
 */
CalendarData.prototype.dateInfo = function (date) {
    let year = date.getFullYear()
    let month = date.getMonth()
    let day = date.getDate()
    let week = date.getDay()
    return {
        date,
        year,
        month: month + 1,
        day,
        dateStr: `${year}-${month + 1}-${day}`,
        week: this.weekContrast(week)
    }
}

CalendarData.prototype.weekContrast = function (week) {
    return new Map([
        [0, '周日'],
        [1, '周一'],
        [2, '周二'],
        [3, '周三'],
        [4, '周四'],
        [5, '周五'],
        [6, '周六']
    ]).get(week)
}

CalendarData.prototype.getData = function () {
    return {
        data: this.createDataArray(),
        dateInfo: this.getDayCount()
    }
}

CalendarData.prototype.createDataArray = function () {
    let { dayCount, year, month } = this.getDayCount()

    // 生成空二维码数组
    // 循环填入相关数据 
    let dataArray = Array(1).fill(null).map(() => Array(7).fill(null))
    let weekIndex = 0
    for (let i = 1; i <= dayCount; i++) {
        let date = new Date(year, month, i)
        let year_str = date.getFullYear()
        let month_str = date.getMonth() + 1
        let day_str = date.getDate()
        let week = date.getDay()
        dataArray[weekIndex][week] = {
            date,
            year_str,
            month_str,
            day_str,
            week: this.weekContrast(week),
            dateStr: `${year_str}-${month_str}-${day_str}`
        }
        if (week == 6) {
            if (i !== dayCount) {
                weekIndex++
                dataArray.push(Array(7).fill(null))
            }
        }
    }

    // 倒叙镜像计算最后一个满足条件的数据
    // 补齐数组头部空键值
    let countBefore = 6 - [...dataArray[0]].reverse().findIndex(it => it == null)
    if (countBefore > 0 && countBefore < 6) {
        for (let i = countBefore; i >= 0; i--) {
            // 明天 - 1 = 今天
            const dayBefore = dataArray[0][i + 1].date
            let newDayBefore = new Date(dayBefore.getFullYear(), dayBefore.getMonth(), dayBefore.getDate())
            if (dayBefore.getDate() == 1) {
                newDayBefore = new Date(newDayBefore.getFullYear(), newDayBefore.getMonth(), 0)
            } else {
                newDayBefore.setDate(dayBefore.getDate() - 1)
            }
            let year_str = newDayBefore.getFullYear()
            let month_str = newDayBefore.getMonth() + 1
            let day_str = newDayBefore.getDate()
            let week = newDayBefore.getDay()
            dataArray[0][i] = {
                date: newDayBefore,
                year_str,
                month_str,
                day_str,
                week: this.weekContrast(week),
                dateStr: `${year_str}-${month_str}-${day_str}`
            }
        }
    }

    // // 补齐数组尾部空键值
    let afterArrIndex = dataArray.length - 1
    let countAfter = dataArray[afterArrIndex].findIndex(it => it == null)
    if (countAfter != -1) {
        for (let i = countAfter; i <= 6; i++) {
            // 昨天 + 1 = 今天 
            let dayAfter = dataArray[afterArrIndex][i - 1].date
            let newDayAfter = new Date(dayAfter.getFullYear(), dayAfter.getMonth(), dayAfter.getDate())
            if (dayAfter.getDate() == dayCount) {
                newDayAfter = new Date(newDayAfter.getFullYear(), newDayAfter.getMonth() + 1, 1)
            } else {
                newDayAfter.setDate(newDayAfter.getDate() + 1)
            }
            let year_str = newDayAfter.getFullYear()
            let month_str = newDayAfter.getMonth() + 1
            let day_str = newDayAfter.getDate()
            let week = newDayAfter.getDay()
            dataArray[afterArrIndex][i] = {
                date: newDayAfter,
                year_str,
                month_str,
                day_str,
                week: this.weekContrast(week),
                dateStr: `${year_str}-${month_str}-${day_str}`
            }
        }
    }

    return dataArray
}

